<template>
  <div>

    <vxe-toolbar ref="toolbarRef" custom>

      <template #buttons>
        <Form inline @submit.native.prevent class="form-search">

          <FormItem >
            <Input v-model="keyword" placeholder="姓名/账号/手机号" style="width: 200px" />
          </FormItem>


          <FormItem>
            <Button type="primary"   @click="SearchUser" icon="md-search">查询</Button>
          </FormItem>

          <FormItem>
            <Button type="primary"  @click="AddData" icon="md-add">新增</Button>
          </FormItem>


        </Form>

      </template>
    </vxe-toolbar>
    <vxe-table ref="xTable" id="user_index" border :height="tableHeight" :loading="loadingTable"
      size="small" align="center" :row-config="{ isHover: true }" :data="tableData"
      show-header-overflow show-overflow="tooltip">
      <vxe-column type="seq" title="序号" width="60">
        <template #default="{ seq }">
          {{ seq + (page - 1) * size }}

        </template>

      </vxe-column>
      <vxe-column title="操作" fixed="right" width="160">
        <template #default="{ row }">
          <Tooltip content="修改" placement="bottom-end">
            <Button type="primary" icon="ios-create" @click="editData(row)" style="margin-right:2px"></Button>
          </Tooltip>
          <Tooltip content="删除" placement="bottom-end">
            <Button type="error" icon="ios-trash" @click="DeleteUser(row.Id)" style="margin-right:2px"></Button>
          </Tooltip>
          <Tooltip content="重置密码" placement="bottom-end">
            <Button icon="ios-unlock" type="warning" @click="ResetUserPwd(row.Id)" style="margin-right:2px"></Button>
          </Tooltip>

        </template>
      </vxe-column>
      <vxe-column field="Status" title="状态" width="100">
        <template #default="{ row }">
          <Tag :color="row.Status == 1 ? 'success' : 'error'">{{ row.Status == 1 ? '正常' : '停用' }}</Tag>
        </template>
      </vxe-column>

      <vxe-column field="Account" title="账号" width="200"> </vxe-column>
      <vxe-column field="Name" title="姓名" width="100"> </vxe-column>
      <vxe-column field="Sex" title="性别" width="60">
        <template #default="{ row }">
          <Tag :color="row.Sex == 1 ? 'green' : 'orange'">{{ row.Sex == 1 ? '男' : '女' }}</Tag>
        </template>
      </vxe-column>
      <vxe-column field="Phone" title="手机" width="150"> </vxe-column>
      <vxe-column field="RoleName" title="角色" width="100"> </vxe-column>
      <vxe-column field="Remark" title="备注"> </vxe-column>


    </vxe-table>

    <Page :total="total" :current="page" :page-size="size" :page-size-opts="[10, 50, 100, 300, 500, 1000, 2000, 5000]"
      @on-change="(s) => {
        page = s;
        SearchUser();
      }
      " @on-page-size-change="(p) => {
        size = p;
        page = 1;
        SearchUser();
      }
      " show-total show-sizer />
    <Modal :width="600" draggable sticky v-model="modalData" :title="tempData.Id > 0 ? '修改' : '新增'"
      :mask-closable="false">
      <Form class="form-more" ref="formValidate" :model="tempData" :label-width="90" :rules="ruleValidate">
        <FormItem label="登录账号" prop="Account">
          <Input v-model="tempData.Account" />
        </FormItem>
        <FormItem label="姓名" prop="Name">
          <Input v-model="tempData.Name" />
        </FormItem>
        <FormItem label="电话" prop="Phone">
          <Input v-model="tempData.Phone" />
        </FormItem>

        <FormItem label="性别" prop="Sex">
          <RadioGroup v-model="tempData.Sex">
            <Radio :label="1">男</Radio>
            <Radio :label="2">女</Radio>
          </RadioGroup>
        </FormItem>
        <FormItem label="角色" prop="RoleId">
          <Select v-model="tempData.RoleId">
            <Option v-for="item in roleList" :value="item.Id" :key="item.Id">{{ item.Name }}</Option>
          </Select>
        </FormItem>

        <FormItem label="备注" prop="Remark">
          <Input v-model="tempData.Remark" />
        </FormItem>

        <FormItem label="状态" prop="Status">
          <RadioGroup v-model="tempData.Status">
            <Radio :label="1">正常</Radio>
            <Radio :label="2">停用</Radio>
          </RadioGroup>
        </FormItem>

      </Form>
      <div slot="footer">
        <Button type="text" @click="modalData = false">取消</Button>
        <Button type="primary" :loading="loading" @click="SaveData">确定</Button>
      </div>
    </Modal>

  </div>
</template>

<script>

import {
  SearchUser,
  SaveUser,
  DeleteUser,
  ResetUserPwd,
  GetRoles,
} from '@/api/rbac'
export default {
  name: 'user',
  data() {
    return {
      accountTypeName: '',

      total: 0,
      page: 1,
      size: 50,
      tableHeight: 450,
      keyword: '',

      roleList: [],
      ruleValidate: {
        Account: [
          {
            required: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ],
        Name: [
          {
            required: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ],
        RoleId: [
          {
            type: 'number',
            required: true,
            message: '不能为空',
            trigger: 'change'
          }
        ],
      },
      loading: false,
      loadingTable: false,
      loadingRole: false,

      tempData: {

      },
      tempIds: [],
      modalData: false,

      tableData: [],

    }
  },
  computed: {
    userType() {
      return this.$store.state.user.userInfo.UserType
    }

  },
  methods: {

    GetRoles() {
      GetRoles(this.type).then((res) => {
        if (res.StatusCode == 200) {
          this.roleList = res.Data
        }
      })
    },
    GetCountyList() {
      GetCountyList(this.type).then((res) => {
        if (res.StatusCode == 200) {
          this.countyList = res.Data
        }
      })
    },


    SaveData() {
      let vm = this
      vm.$refs['formValidate'].validate((valid) => {
        if (valid) {
          vm.loading = true
          SaveUser(vm.tempData).then((res) => {
            vm.loading = false
            if (res.StatusCode == 200) {
              vm.modalData = false
              vm.$Message.success({ title: '提示', content: '保存成功' })
              vm.SearchUser()
            } else {
              vm.$Message.error({ title: '提示', content: data.Errors })
            }
          })
        }
      })
    },


    SearchUser() {
      let vm = this
      vm.loadingTable = true
      var reqData = {
        page: vm.page,
        size: vm.size,
        keyword: vm.keyword,

      }
      SearchUser(reqData).then((res) => {
        vm.loadingTable = false

        if (res.StatusCode == 200) {
          vm.tableData = res.Data.Items
          vm.total = res.Data.Total
        } else {
          vm.$Notice.error({
            title: data.Errors
          })
        }
      })
    },

    DeleteUser(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定要删除吗？',
        onOk: () => {
          DeleteUser(id).then((res) => {
            if (res.StatusCode == 200) {
              this.SearchUser()
              this.$Message.success({ title: '提示', content: '删除成功' })
            } else {
              this.$Message.error({ title: '提示', content: data.Errors })
            }
          })
        },
        onCancel: () => { }
      })
    },

    ResetUserPwd(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定要重置密码？',
        onOk: () => {
          ResetUserPwd(id).then((res) => {
            if (res.StatusCode == 200) {
              this.$Notice.success({
                title: '重置成功',
                desc: '密码已重置,为确保账号安全，请及时修改。',
                duration: 2
              })
            } else {
              this.$Message.error({ title: '提示', content: data.Errors })
            }
          })
        },
        onCancel: () => { }
      })
    },

    AddData() {
      var vm = this

      vm.tempData = {
        Sex: 1,
        Status: 1,
        RoleId: vm.roleList[0].Id,
      }

      vm.modalData = true
    },
    editData(row) {
      this.tempData = row
      this.modalData = true
    },
    GrantMenu(id) {
      this.tempIds = [id]
      this.modalMenu = true
    },
    GrantMutiMenu() {
      var vm = this
      var selectRecords = this.$refs.xTable.getCheckboxRecords()

      if (selectRecords.length == 0) {
        vm.$Message.warning('请选择至少一条数据')
        return
      }
      this.tempIds = selectRecords.map(x => x.Id)
      this.modalMenu = true
    }
  },

  mounted() {
    this.tableHeight = window.innerHeight - 240
    const $table = this.$refs.xTable
    const $toolbar = this.$refs.toolbarRef
    if ($table && $toolbar) {
      $table.connect($toolbar)
    }
    this.GetRoles()
    this.SearchUser()

  }
}
</script>
